package com.cby.operation.anti.data

import android.text.TextUtils
import com.cby.operation.anti.AntiAddictManager
import com.cby.operation.common.http.HttpHelper
import com.cby.operation.common.util.CbyLogUtils
import com.cby.operation.common.util.ThreadPoolUtils
import java.util.*

/**
 * @description: AntiAddictApiRepository
 *
 * @author ChenBaoyang
 * @date 8/2/21 17:31
 */
internal object AntiAddictApiRepository {

    private const val TAG = "AntiAddictApiRepository"

    private const val ANTI_VALID_URL = "https://apps.4399.com/hykb/heartbeat"

    private const val KET_PTID = "ptid"
    private const val VALUE_PTID = "1020"

    private const val KEY_GAME_ID = "gid"
    private const val KEY_USER_ID = "uid"
    private const val KEY_TOKEN = "token"
    private const val KEY_DURATION = "duration"


    @Throws(Exception::class)
    fun requestFcmData(gameId: String, userId: String, token: String?, duration: Long) {
        val jsonParam: MutableMap<String, Any> = HashMap()
        jsonParam[KEY_GAME_ID] = gameId
        jsonParam[KEY_USER_ID] = userId
        jsonParam[KEY_TOKEN] = token?:""
        jsonParam[KEY_DURATION] = duration
        jsonParam[KET_PTID] = VALUE_PTID
        ThreadPoolUtils.execute{
            HttpHelper.requestGet(ANTI_VALID_URL,
                jsonParam,
                object : HttpHelper.OnHttpRequestListener {
                    override fun onResponseSuccess(response: String?) {
                        try {
                            CbyLogUtils.e(TAG, response)
                            if (TextUtils.isEmpty(response)) {
                                return
                            }
                            AntiAddictManager.instance.parseResponse(response)
                        } catch (e: Exception) {
                            CbyLogUtils.e(TAG, "errorMessage=" + e.message)
                        }
                    }

                    override fun onResponseError(code: Int, errorMessage: String?) {
                        CbyLogUtils.e(TAG, "error-$code,errorMessage=$errorMessage")
                    }

                })
        }
    }
}